<?php

namespace app\cdc\validate\admin;

use app\common\model\mysql\TeamGoodsModel;

class PurchaseValidate extends \app\common\validate\BaseValidate
{
    protected $rule = [
        'name' => 'require|checkName',
        'category' => 'require',
        'sub_category_id' => 'require|array',
        'price' => 'require',
        'price_init_market' => 'require',
        'price_ratio' => 'require',
        'profit_num' => 'require',
    ];

    protected $message = [
        'name.require' => '商品名称 必填',
        'category.require' => '商品分类 必选',
        'sub_category_id.require' => '商品子分类 必选',
        'sub_category_id.array' => '商品子分类是数组',
        'price.require' => '成本价 必填',
        'price_init_market.require' => '市场价 必填',
        'price_ratio.require' => '系数 必选',
        'profit_num.require' => '分润值 必选',
    ];

    protected $scene = [
        'save' => ['name', 'category', 'sub_category_id', 'price', 'price_ratio', 'profit_num', 'price_init_market'],
    ];

    protected function checkName($value, $rule, $data = [])
    {
        $where[] = ['name', '=', $data['name']];
        !empty($data['id']) ? $where[] = ['id', '<>', $data['id']] : false;
        if (TeamGoodsModel::checkRowExists($where)) {
            return '商品名称已存在';
        }
        return true;
    }
}